'use strict';

angular.module('CBO').controller('WorkLocationController',
    ['$scope', '$stateParams', 'PublicModule', 'PublicFunction', '$location', 'Authentication',
    function($scope, $stateParams, PublicModule, PublicFunction, $location, Authentication) {
        //$scope.authentication = Authentication;
        
        $scope.btnSave = function(){
            if($scope.Module.WorkLocation.FocusedRecord.Code === undefined || 
                $scope.Module.WorkLocation.FocusedRecord.Code.trim() === ""){
                PublicFunction.alert("编码不能为空！", "保存错误");
                return;
            }
            if($scope.Module.WorkLocation.FocusedRecord.Name === undefined || 
                $scope.Module.WorkLocation.FocusedRecord.Name.trim() === ""){
                PublicFunction.alert("名称不能为空！", "保存错误");
                return;
            }

            $scope.Module.WorkLocation.save();
        };
        $scope.btnCancel = function(){
            $scope.Module.WorkLocation.cancel();
        };
        $scope.btnAdd = function(){
            $scope.Module.WorkLocation.add();
            $scope.Module.WorkLocation.FocusedRecord.IsEffective = true;
        };
        $scope.btnDelete = function(){
            $scope.Module.WorkLocation.delete();
        };
        $scope.btnList = function(){
            $location.url('/index/WorkLocation/List');
        };
        $scope.btnFirstPage = function(){
            $scope.Module.WorkLocation.firstPage();
        };
        $scope.btnPrePage = function(){
            $scope.Module.WorkLocation.prePage();
        };
        $scope.btnNextPage = function(){
            $scope.Module.WorkLocation.nextPage();
        };
        $scope.btnLastPage = function(){
            $scope.Module.WorkLocation.lastPage();
        };

        //初始化
        PublicModule.initModule($scope, 'WorkLocation');
        $scope.Module.WorkLocation.QueryParameter.Populate = {path: 'Department', select: 'Code Name'};
        //返回单个ref的全部数据："Department" 或者 {path: 'Department'}
        //返回多个ref的全部数据："Department CreatedBy" 或者 [{path: 'Department'}, {path: 'CreatedBy'}]
        //返回单个ref的多个指定字段: {path: 'Department', select: 'Code Name'}
        //返回多个ref的多个指定字段：[{path: 'Department', select: 'Code Name'}, {path: 'CreatedBy', select: 'username QQ'}]

        if($stateParams.id) {
            $scope.Module.WorkLocation.findById($stateParams.id, function(record){
                $scope.Module.WorkLocation.FocusedRecord = record;
            });
        }
        else{
            $scope.btnAdd();
        }
    }
]);
